package com.microservice.salmon.fastdfs.client;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * FastDFSClient 客户端， 使用的使用 需要注入到spring IOC容器中
 * 确保此类被spring扫描到即可
 *
 * FastFileStorageClient 这个类在spring容器中已经注入了 DefaultFastFileStorageClient
 *
 */
@Component
public class FastDFSClientImpl implements FastDFSClient {

	private FastFileStorageClient storageClient;

	public FastDFSClientImpl(FastFileStorageClient storageClient) {
		this.storageClient = storageClient;
	}
	/**
	 * 上传文件
	 * 
	 * @param file
	 *            文件对象
	 * @return 文件访问地址
	 * @throws IOException
	 */
	@Override
	public String uploadFile(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
				FilenameUtils.getExtension(file.getOriginalFilename()), null);
		return storePath.getPath();
	}

	/**
	 * 上传文件并生成缩略图
	 * @param file
	 *            文件对象
	 * @return 文件访问地址
	 * @throws IOException
	 */
	@Override
	public String uploadFile2(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
				FilenameUtils.getExtension(file.getOriginalFilename()), null);
		return storePath.getPath();
	}
	
	@Override
	public String uploadQRCode(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
				"png", null);
		return storePath.getPath();
	}
	
	@Override
	public String uploadFace(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
				"png", null);
		return storePath.getPath();
	}

	/**
	 * 上传base64格式的数据, 并生成缩略图
	 * @param file MultipartFile 文件
	 * @return
	 * @throws IOException
	 */
	@Override
	public String uploadBase64(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
				"png", null);
		return storePath.getPath();
	}


	/**
	 * 将一段字符串生成一个文件上传
	 *
	 * @param content
	 *            文件内容
	 * @param fileExtension 文件扩展名
	 * @return
	 */
	public String uploadFile(String content, String fileExtension) {
		byte[] buff = content.getBytes(Charset.forName("UTF-8"));
		return uploadFile(buff, fileExtension);
	}

	public String uploadFile(byte[] buff, String fileExtension) {
		return uploadFile(buff, fileExtension, false);
	}

	@Override
	public String uploadFile(byte[] buff, String fileExtension, boolean containsThumb) {
		ByteArrayInputStream stream = new ByteArrayInputStream(buff);
		StorePath storePath = containsThumb ?
				storageClient.uploadImageAndCrtThumbImage(stream, buff.length, fileExtension, null)
				: storageClient.uploadFile(stream, buff.length, fileExtension, null);
		return storePath.getPath();
	}

	/**
	 * 删除文件
	 * 
	 * @param fileUrl
	 *            文件访问地址
	 * @return
	 */
	@Override
	public void deleteFile(String fileUrl) {
		if (StringUtils.isEmpty(fileUrl)) {
			return;
		}
		try {
			StorePath storePath = StorePath.parseFromUrl(fileUrl);
			storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
		} catch (FdfsUnsupportStorePathException e) {
			e.getMessage();
		}
	}


	/**
	 * 下载文件
	 * @param fileUrl  文件访问地址
	 */
	@Override
	public InputStream downloadFile(String fileUrl) {
		try {
			StorePath storePath = StorePath.parseFromUrl(fileUrl);
			return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(),(ins) -> ins);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
